package task3

import grails.test.mixin.Mock
import grails.test.mixin.TestFor

/**
 * See the API for {@link grails.test.mixin.domain.DomainClassUnitTestMixin} for usage instructions
 */
@TestFor(Project)
@Mock([Department])
class ProjectTests {

    void testConstrains() {
        mockForConstraintsTests(Project)
        def dep_1 = new Department(name: "Department1")
        dep_1.save()

        def proj_1 = new Project(name: "Project1", department: dep_1)
        assertTrue proj_1.validate()
        proj_1.save()

        assertEquals 1, Project.list().size()

        def proj_2 = new Project(name: "Project1", department: dep_1)
        assertFalse proj_2.validate()
        assertEquals "unique", proj_2.errors["name"]

        proj_2.name =""
        assertFalse proj_2.validate()
        assertEquals "blank", proj_2.errors["name"]

        proj_2.name ="Pr"
        assertFalse proj_2.validate()
        assertEquals "size", proj_2.errors["name"]

        proj_2.name ="P"*21
        assertFalse proj_2.validate()
        assertEquals "size", proj_2.errors["name"]

        proj_2.name ="Project2"
        assertTrue proj_2.validate()
        proj_2.save()

        assertEquals 2, Project.list().size()

        def dep_2 = new Department(name: "Department2")
        dep_1.save()

        def proj_3 = new Project(name: "Project1", department: dep_2)
        assertTrue proj_3.validate()
        proj_3.save()

        assertEquals 3, Project.list().size()

        def proj_4 = new Project(name: "Project1", department: dep_2)
        assertFalse proj_4.validate()
        assertEquals "unique", proj_4.errors["name"]

    }
}
